<?php

namespace app\api\controller;

use app\api\model\Good as ModelGood;
use frame\lib\Db;

class Good extends Base
{
    // 商品列表
    public function list()
    {
        $list = Db::name('good')->where(['user_id' => $this->user_id,'delete_time' => 0,'status' => 1])->select();
        success($list);
    }
    public function buy()
    {
        $param = $_GET;
        if(!array_key_exists('id',$param)) error('参数id不能为空',201);
        $good = Db::name('good')->where(['user_id' => $this->user_id , 'id' => $param['id'] , 'status' => 1,'delete_time' => 0])->find();
        if(!$good) error('商品不存在');
        $userData = Db::name('user')->where(['id' => $this->uid])->find();
        $beLeftOver = $userData['score'] - $good['score'];
        if($beLeftOver < 0) error('购买失败：商品需要' . $good['score'] . '积分 , 当前积分只有' . $userData['score']);
        $good['beLeftOver'] = $beLeftOver;
        ModelGood::buy($good);
    }
}
